/**
 * Copyright (C) 2009 Bruno Dumas (bruno.dumas -at- unifr.ch), DIVA group, University of Fribourg, Switzerland
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

//SMUIML v0.4

package diuf.diva.hephaistk.xml.smuiml;

import it.unina.lab.costants.Constants;
import diuf.diva.hephaistk.xml.Attribute;
import diuf.diva.hephaistk.xml.HephaisTKParsingException;
import diuf.diva.hephaistk.xml.XmlGeneric;

public class Integration_Description extends Smuiml_Generic {
	/**
	 *
	 */
	private static final long serialVersionUID = 353622570989434643L;

	public final static String NAME = "integration_description";

	private Recognizers recognizers = null;
	private Triggers triggers = null;
	private Actions actions = null;
	private Dialog dialog = null;

	private Attribute attClient = null;

	public Integration_Description(){
		super();
		setName(NAME);
	}

	/**
	 * @return the attName
	 */
	public Attribute getAttClient() {
		return attClient;
	}

	/**
	 * @param attName the attName to set
	 */
	public void setAttClient(String attClient) {
		this.attClient = new Attribute("client", attClient);
	}

	/**
	 * @return the events
	 */
	public Triggers getTriggers() {
		return triggers;
	}

	/**
	 * @param events the events to set
	 */
	public void setTriggers(Triggers triggers) {
		this.triggers = triggers;
	}

	/**
	 * @return the actions
	 */
	public Actions getActions() {
		return actions;
	}

	/**
	 * @param actions the actions to set
	 */
	public void setActions(Actions actions) {
		this.actions = actions;
	}

	/**
	 * @return the recognizers
	 */
	public Recognizers getRecognizers() {
		return recognizers;
	}

	/**
	 * @param modalities the recognizers to set
	 */
	public void setRecognizers(Recognizers recognizers) {
		this.recognizers = recognizers;
	}

	/**
	 * @return the contexts
	 */
	public Dialog getDialog() {
		return dialog;
	}

	/**
	 * @param contexts the contexts to set
	 */
	public void setDialog(Dialog dialog) {
		this.dialog = dialog;
	}

	public void addElement(XmlGeneric element) throws HephaisTKParsingException {
		if(element instanceof Recognizers){
			this.setRecognizers((Recognizers)element);
		}else if(element instanceof Triggers){
			this.setTriggers((Triggers)element);
		}else if(element instanceof Actions){
			this.setActions((Actions)element);
		}else if(element instanceof Dialog){
			this.setDialog((Dialog)element);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected element: "+element.getName());
		}
	}

	public void addAttribute(Attribute attribute) throws HephaisTKParsingException{
		super.addAttribute(attribute);
		String attName = attribute.getName();
		if (attName.indexOf(':') != -1) {
			attName = attName.substring(attName.indexOf(':')+1);
		}
		addAttribute(attName, attribute.getValue());
	}

	public void addAttribute(String name, String value) throws HephaisTKParsingException{
		if(name.equalsIgnoreCase("client")){
			setAttClient(value);
		}else{
			throw new HephaisTKParsingException(getNamespace()+"."+NAME+" -- unexpected attribute: "+name);
		}
	}

	public String toString(int indent) {
		StringBuilder ret = new StringBuilder();
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("<"+NAME+(attClient != null? " client=\""+attClient.getValue()+"\"" : "")+">\n");
		if(recognizers != null){
			ret.append(recognizers.toString(indent+Constants.INDENT_PREFIX));
		}
		if(triggers != null){
			ret.append(triggers.toString(indent+Constants.INDENT_PREFIX));
		}
		if(actions != null){
			ret.append(actions.toString(indent+Constants.INDENT_PREFIX));
		}
		if(dialog != null){
			ret.append(dialog.toString(indent+Constants.INDENT_PREFIX));
		}
		for(int i = 0; i < indent; i++){
			ret.append(' ');
		}
		ret.append("</"+NAME+">\n");
		return ret.toString();
	}
}
